package rexsee.location;

import rexsee.core.browser.Browser;
import rexsee.core.browser.clazz.JavascriptInterface;
import android.content.Context;
import android.telephony.CellLocation;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.telephony.cdma.CdmaCellLocation;
import android.telephony.gsm.GsmCellLocation;

public class RexseeCellLocation implements JavascriptInterface {

	public static final String INTERFACE_NAME = "CellLocation";
	@Override
	public String getInterfaceName() {
		return mBrowser.application.resources.prefix + INTERFACE_NAME;
	}
	@Override
	public JavascriptInterface getInheritInterface(Browser childBrowser) {
		return this;
	}
	@Override
	public JavascriptInterface getNewInterface(Browser childBrowser) {
		return new RexseeCellLocation(childBrowser);
	}

	public static final String EVENT_ONCELLLOCATIONCHANGED = "onCellLocationChanged";

	public final Context mContext;
	private final Browser mBrowser;
	private final int mPhoneType;
	private PhoneStateListener mListener = null;
	private CellLocation mLocation = null;

	public RexseeCellLocation(Browser browser) {
		mContext = browser.getContext();
		mBrowser = browser;
		browser.eventList.add(EVENT_ONCELLLOCATIONCHANGED);
		TelephonyManager tm = (TelephonyManager) mContext.getSystemService(Context.TELEPHONY_SERVICE);
		mPhoneType = tm.getPhoneType();
	}

	//JavaScript Interface
	public boolean isEnabled() {
		return mListener != null;
	}
	public boolean enable() {
		try {
			TelephonyManager tm = (TelephonyManager) mContext.getSystemService(Context.TELEPHONY_SERVICE);
			mListener = new PhoneStateListener() {
				@Override
				public void onCellLocationChanged(CellLocation location) {
					mLocation = location;
					mBrowser.eventList.run(EVENT_ONCELLLOCATIONCHANGED);
				}
			};
			tm.listen(mListener, PhoneStateListener.LISTEN_CELL_LOCATION);
			return true;
		} catch (Exception e) {
			mBrowser.exception(getInterfaceName(), e);
			return false;
		}
	}
	public boolean disable() {
		if (mListener == null) return true;
		try {
			TelephonyManager tm = (TelephonyManager) mContext.getSystemService(Context.TELEPHONY_SERVICE);
			tm.listen(mListener, PhoneStateListener.LISTEN_NONE);
			mListener = null;
			return true;
		} catch (Exception e) {
			mBrowser.exception(getInterfaceName(), e);
			return false;
		}
	}
	public String getLastKnownLocation() {
		if (mLocation == null) return "{}";
		TelephonyManager tm = (TelephonyManager) mContext.getSystemService(Context.TELEPHONY_SERVICE);
		String rtn = "";
		rtn += "\"operator\":\"" + tm.getNetworkOperator() + "\"";
		rtn += ",\"operatorName\":\"" + tm.getNetworkOperatorName() + "\"";
		if (mPhoneType == TelephonyManager.PHONE_TYPE_GSM) {
			GsmCellLocation gsm = (GsmCellLocation) mLocation;
			rtn += ",\"type\":\"GSM\"";
			rtn += ",\"cid\":" + gsm.getCid();
			rtn += ",\"lac\":" + gsm.getLac();
		} else if (mPhoneType == TelephonyManager.PHONE_TYPE_CDMA) {
			CdmaCellLocation cdma = (CdmaCellLocation) mLocation;
			rtn += ",\"type\":\"CDMA\"";
			rtn += ",\"baseStationId\":" + cdma.getBaseStationId();
			rtn += ",\"baseStationLatitude\":" + cdma.getBaseStationLatitude();
			rtn += ",\"baseStationLongitude\":" + cdma.getBaseStationLongitude();
			rtn += ",\"networkId\":" + cdma.getNetworkId();
			rtn += ",\"systemId\":" + cdma.getSystemId();
		}
		return "{" + rtn + "}";
	}

}
